﻿// IOOutputDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "IOOutputDlg.h"
#include "afxdialogex.h"


// CIOOutputDlg 对话框

IMPLEMENT_DYNAMIC(CIOOutputDlg, CDialogEx)

CIOOutputDlg::CIOOutputDlg(CWnd* pParent /*=nullptr*/)
	//: CDialogEx(IDD_DIALOG_IO_OUTPUT, pParent)
{

}

CIOOutputDlg::~CIOOutputDlg()
{
}

void CIOOutputDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CIOOutputDlg, CDialogEx)
	ON_WM_TIMER()
	ON_CONTROL_RANGE(BN_CLICKED, IDC_CHECK_OUT_0, IDC_CHECK_OUT_64, &CIOOutputDlg::OnBnClickedOption)
	ON_WM_CONTEXTMENU()
	ON_WM_DESTROY()
	ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()


void CIOOutputDlg::SetIoStatus()
{
	CButton* pCheck = nullptr;
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_0);  pCheck->SetCheck(gSystemMgr.Y00);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_1);  pCheck->SetCheck(gSystemMgr.Y01);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_2);  pCheck->SetCheck(gSystemMgr.Y02);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_3);  pCheck->SetCheck(gSystemMgr.Y03);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_4);  pCheck->SetCheck(gSystemMgr.Y04);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_5);  pCheck->SetCheck(gSystemMgr.Y05);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_6);  pCheck->SetCheck(gSystemMgr.Y06);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_7);  pCheck->SetCheck(gSystemMgr.Y07);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_8);  pCheck->SetCheck(gSystemMgr.Y08);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_9);  pCheck->SetCheck(gSystemMgr.Y09);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_10);  pCheck->SetCheck(gSystemMgr.Y10);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_11);  pCheck->SetCheck(gSystemMgr.Y11);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_12);  pCheck->SetCheck(gSystemMgr.Y12);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_13);  pCheck->SetCheck(gSystemMgr.Y13);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_14);  pCheck->SetCheck(gSystemMgr.Y14);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_15);  pCheck->SetCheck(gSystemMgr.Y15);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_16);  pCheck->SetCheck(gSystemMgr.Y16);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_17);  pCheck->SetCheck(gSystemMgr.Y17);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_18);  pCheck->SetCheck(gSystemMgr.Y18);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_19);  pCheck->SetCheck(gSystemMgr.Y19);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_20);  pCheck->SetCheck(gSystemMgr.Y20);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_21);  pCheck->SetCheck(gSystemMgr.Y21);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_22);  pCheck->SetCheck(gSystemMgr.Y22);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_23);  pCheck->SetCheck(gSystemMgr.Y23);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_24);  pCheck->SetCheck(gSystemMgr.Y24);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_25);  pCheck->SetCheck(gSystemMgr.Y25);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_26);  pCheck->SetCheck(gSystemMgr.Y26);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_27);  pCheck->SetCheck(gSystemMgr.Y27);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_28);  pCheck->SetCheck(gSystemMgr.Y28);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_29);  pCheck->SetCheck(gSystemMgr.Y29);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_30);  pCheck->SetCheck(gSystemMgr.Y30);
	pCheck = (CButton*)GetDlgItem(IDC_CHECK_OUT_31);  pCheck->SetCheck(gSystemMgr.Y31);	
}




BOOL CIOOutputDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	SetTimer(TIMER_ID_IO_CONTROL_STATUS_OUT, 50, NULL);

	ChangeIoTextOut();
	SetIoStatus();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CIOOutputDlg::OnCancel()
{
	KillTimer(TIMER_ID_IO_CONTROL_STATUS_OUT);

	CDialogEx::OnCancel();
}


void CIOOutputDlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == TIMER_ID_IO_CONTROL_STATUS_OUT)
	{
		SetIoStatus();
	}

	CDialogEx::OnTimer(nIDEvent);
}

void CIOOutputDlg::ChangeIoTextOut()
{
	std::vector<IO_MODULE> vIoMap;
	vIoMap = *gConfigMgr.GetIOOutputSetting();
	for (int i = 0; i < 64; i++)
	{
		int cnt;
		cnt = i;
		CWnd * pStatic = nullptr;
		pStatic = (CWnd*)GetDlgItem(IDC_STATIC_OUT0 + cnt);//控件编号问题
		CString cstr;
		cstr.Format(L"%d_", i);
		cstr.Append(vIoMap[i].m_cstrDescription);
		pStatic->SetWindowTextW(cstr);

	}
}

void CIOOutputDlg::OnBnClickedOption(UINT nID)
{
	int nPort = nID - IDC_CHECK_OUT_0;
	//nPort = nPort / 2;
	CString cstr;
	cstr.Format(L"%d", nPort);

	CButton* pCheckBox = (CButton*)GetDlgItem(nID);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		gSystemMgr.m_cIo.SetOutputIo(nPort, ON);
		cstr.Append(L"选中");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
	}
	if (nFlag == BST_UNCHECKED)
	{
		gSystemMgr.m_cIo.SetOutputIo(nPort, OFF);
		cstr.Append(L"取消");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
	}
}

